import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _isEmpty from "lodash/isEmpty";

var _dec, _class;

import React, { PureComponent } from 'react';
import { Button, Form, Icon, Input, Modal, Select, Tabs, Upload } from 'antd';
import isAbsoluteUrl from 'is-absolute-url';
import intl from "../../utils/intl";
import request from "../../utils/request";
import { getEnvConfig } from "../../utils/iocUtils";
import notification from "../../utils/notification";
import { getAttachmentUrl } from "../../utils/utils"; // import intl from 'utils/intl';

var TabPane = Tabs.TabPane;
var FormItem = Form.Item;
var Option = Select.Option;
var formItemLayout = {
  labelCol: {
    xs: {
      span: 24
    },
    sm: {
      span: 4
    }
  },
  wrapperCol: {
    xs: {
      span: 24
    },
    sm: {
      span: 20
    }
  }
};
var RichTextInsertImages = (_dec = Form.create({
  fieldNameProp: null
}), _dec(_class = /*#__PURE__*/function (_PureComponent) {
  _inherits(RichTextInsertImages, _PureComponent);

  var _super = _createSuper(RichTextInsertImages);

  function RichTextInsertImages() {
    var _this;

    _classCallCheck(this, RichTextInsertImages);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      fileList: [],
      fileUrl: null
    };
    _this.config = getEnvConfig();
    return _this;
  }

  _createClass(RichTextInsertImages, [{
    key: "ok",
    value: function ok() {
      var _this$props = this.props,
          _this$props$form$getF = _this$props.form.getFieldValue,
          getFieldValue = _this$props$form$getF === void 0 ? function (e) {
        return e;
      } : _this$props$form$getF,
          onOk = _this$props.onOk;
      var _this$state = this.state,
          fileList = _this$state.fileList,
          fileUrl = _this$state.fileUrl;
      var content = '';

      if (!_isEmpty(fileList) && fileUrl) {
        content += "<a href=\"".concat(fileUrl, "\" target=\"_blank\">").concat(getFieldValue('fileName') || fileList[0].name || fileUrl, "</a>");
      }

      var url = getFieldValue('url');
      var urlPrefix = getFieldValue('urlPrefix');

      if (!_isEmpty(url)) {
        content += "<a href=\"".concat(urlPrefix + url, "\" target=\"_blank\">").concat(getFieldValue('fileName') || url + urlPrefix, "</a>");
      }

      onOk(content);
      this.cancel();
    }
  }, {
    key: "cancel",
    value: function cancel() {
      var _this$props$onCancel = this.props.onCancel,
          onCancel = _this$props$onCancel === void 0 ? function (e) {
        return e;
      } : _this$props$onCancel;
      this.setState({
        fileList: []
      });
      onCancel();
    }
  }, {
    key: "onUploadRemove",
    value: function onUploadRemove(file) {
      var fileList = this.state.fileList;
      this.setState({
        fileList: fileList.filter(function (o) {
          return o.uid !== file.uid;
        })
      });
    }
  }, {
    key: "setFileList",
    value: function setFileList(file) {
      this.setState({
        fileList: [file]
      });
    }
  }, {
    key: "beforeUpload",
    value: function beforeUpload(file) {
      var _this2 = this;

      var _getEnvConfig = getEnvConfig(),
          BKT_PUBLIC = _getEnvConfig.BKT_PUBLIC,
          CPAAS_FILE = _getEnvConfig.CPAAS_FILE;

      var _this$props2 = this.props,
          _this$props2$bucketNa = _this$props2.bucketName,
          bucketName = _this$props2$bucketNa === void 0 ? BKT_PUBLIC : _this$props2$bucketNa,
          _this$props2$bucketDi = _this$props2.bucketDirectory,
          bucketDirectory = _this$props2$bucketDi === void 0 ? 'editor' : _this$props2$bucketDi;
      var formData = new FormData();
      formData.append('file', file, file.name);
      formData.append('bucketName', bucketName);
      formData.append('directory', bucketDirectory);
      formData.append('fileName', file.name);
      this.setState({
        fileList: [file]
      });
      request("".concat(CPAAS_FILE, "/v1/files/multipart"), {
        method: 'POST',
        body: formData,
        type: 'FORM',
        processData: false,
        responseType: 'text'
      }).then(function (res) {
        if (isAbsoluteUrl(res)) {
          notification.success();

          _this2.setState({
            fileUrl: getAttachmentUrl(res, BKT_PUBLIC, undefined, bucketDirectory)
          });
        } else {
          notification.error({
            description: JSON.parse(res).message
          });
        }
      });
      return false;
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          visible = _this$props3.visible,
          form = _this$props3.form,
          _this$props3$prompt = _this$props3.prompt,
          prompt = _this$props3$prompt === void 0 ? {} : _this$props3$prompt;
      var getFieldDecorator = form.getFieldDecorator;
      var fileList = this.state.fileList;
      var uploadProps = {
        beforeUpload: this.beforeUpload.bind(this),
        onRemove: this.onUploadRemove.bind(this),
        fileList: fileList
      };
      var prefixSelector = getFieldDecorator("urlPrefix", {
        initialValue: 'https://'
      })( /*#__PURE__*/React.createElement(Select, null, /*#__PURE__*/React.createElement(Option, {
        value: "https://"
      }, "https://"), /*#__PURE__*/React.createElement(Option, {
        value: "http://"
      }, "http://")));
      return /*#__PURE__*/React.createElement(Modal, {
        title: prompt.insertFile || intl.get('cpaas.common.richTextEditor.insertFile').d('插入文件'),
        visible: visible,
        onOk: this.ok.bind(this),
        onCancel: this.cancel.bind(this),
        destroyOnClose: true,
        width: 500
      }, /*#__PURE__*/React.createElement(Tabs, {
        defaultActiveKey: "upload"
      }, /*#__PURE__*/React.createElement(TabPane, {
        tab: /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement(Icon, {
          type: "upload"
        }), prompt.uploadLocalFile || intl.get('cpaas.common.richTextEditor.localUpload').d('本地上传')),
        key: "upload",
        style: {
          height: '100%'
        }
      }, /*#__PURE__*/React.createElement(Upload, uploadProps, /*#__PURE__*/React.createElement(Button, null, /*#__PURE__*/React.createElement(Icon, {
        type: "upload"
      }), prompt.selectFile || intl.get('cpaas.common.richTextEditor.selectUpload').d('选择文件上传'))), /*#__PURE__*/React.createElement("br", null)), /*#__PURE__*/React.createElement(TabPane, {
        tab: /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement(Icon, {
          type: "link"
        }), prompt.fileLink || intl.get('cpaas.common.richTextEditor.fileLink').d('文件链接')),
        key: "link"
      }, /*#__PURE__*/React.createElement(Form, null, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formItemLayout, {
        label: "url"
      }), getFieldDecorator("url")( /*#__PURE__*/React.createElement(Input, {
        addonBefore: prefixSelector,
        style: {
          width: '80%'
        }
      }))), /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formItemLayout, {
        label: prompt.fileDescription || intl.get('cpaas.common.richTextEditor.fileDescription').d('文件说明')
      }), getFieldDecorator("fileName")( /*#__PURE__*/React.createElement(Input, {
        style: {
          width: '80%'
        }
      })))))));
    }
  }]);

  return RichTextInsertImages;
}(PureComponent)) || _class);
export { RichTextInsertImages as default };